package com.bak.tt.service.impl;

import com.bak.tt.dto.TransferReqDTO;
import com.bak.tt.manager.AccountManager;
import com.bak.tt.model.Account;
import com.bak.tt.service.TransferService;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;

/**
 * Created by yingsy on 2021-01-26.
 */
@Slf4j
public class TransferServiceImpl implements TransferService {

    private AccountManager accountManager = new AccountManager();

    @Override
    public void transfer(TransferReqDTO reqDTO) throws SQLException, ClassNotFoundException {

        log.info("Transfer req:{}", reqDTO);
        Account from = accountManager.queryAccountByCardNo(reqDTO.getFromCardNo());
        Account to = accountManager.queryAccountByCardNo(reqDTO.getToCardNo());

        from.setMoney(from.getMoney() - reqDTO.getMoney());
        to.setMoney(to.getMoney() + reqDTO.getMoney());

        accountManager.updateAccountByCardNo(from);
        accountManager.updateAccountByCardNo(to);
    }
}
